
function adjustHeight(){
	var height = $(window).height();
	var topH = $("#top").outerHeight();
	var pagetitleH = $("#pagetitle").outerHeight();
	var bottomH = $("#bottom").outerHeight();
	var h = height - topH - pagetitleH - bottomH;
	$("#page").height(Math.max(h, 400));
}
function adjustWidth(){
	/*var width = $(window).width();
	var leftW = $("#left-menu-tree").width();
	var resizeW = $("#resize-bar").width();
	$("#content-frame").width(width - leftW - resizeW);
	//alert($("#content-frame").width());*/ 
}

function showMenu(){
	var temp = window.USER_MENU_TREE;
	if(temp){
		var menubar = [];
		menubar.push('<li id="m_homepage" class="current"><a href="javascript:doAction(\'/home.jsp\', \'首页\', \'homepage\', -1);">首页</a></li>');
		var first;
		for(var i=0; i<temp.length; i++){
			var m = temp[i];
			if(i == 0){first = m;}
			menubar.push("<li id='m_"+m.code+"'>");
			if(m.subMenus){
				menubar.push("<a href='javascript:doAction(\""+m.url+"\", \""+m.name+"\", \""+m.code+"\", "+i+")'>"+m.name+"</a>");
			}else{
				menubar.push("<a href='javascript:doAction(\""+m.url+"\", \""+m.name+"\", \""+m.code+"\", -1)'>"+m.name+"</a>");
			}
			menubar.push("</li>");
			
			window.MENU_NAME_MAP[m.name] = {id: m.code, url: m.url};
		}
		$("#MENU_BAR").html(menubar.join(''));
	}
}

function loadSubMenus(menu, pname){
	var subMenus = menu.subMenus;
	var menus = []; //存放二级菜单及其子菜单
	for(var i=0; i<subMenus.length; i++){
		var m = subMenus[i];
		menus.push("<li id='m_"+m.code+"'>");
		var mname = m.name;
		if(m.url){
			menus.push("<a href='javascript:doAction(\""+m.url+"\", \""+mname+"\", \""+m.code+"\")'>"+m.name+"</a>");
		}else{
			menus.push("<a href='javascript:void(0)'>"+m.name+"</a>");
		}
		if(m.subMenus){
			menus.push('<ul>');
			menus.push(loadSubMenus(m, mname));
			menus.push('</ul>');
		}
		menus.push("</li>");
		
		window.MENU_NAME_MAP[m.name] = {id: m.code, url: m.url};
	}
	return menus.join('');
}

function setNavMenuStyle(id, containerId){
	if(id){
		$("#"+containerId+" li").removeClass("current");
		var node = document.getElementById("m_"+id);
		if(node){
			while(node.id != containerId){
				if(node.tagName == 'LI'){
					$(node).addClass("current");
				}
				node = node.parentNode;
			}
		}
	}
}

function setLeftMenuStyle(id, containerId){
	if(id){
		$("#"+containerId+" a").removeClass("current");
		var node = document.getElementById("m_"+id);
		if(node){
			$(node).find("a:first").addClass("current");
		}
	}
}

function doAction(url, menuName, id, index){
	if(typeof index != 'undefined'){
		if(index != -1){
			var menus = loadSubMenus(USER_MENU_TREE[index]);
			$("#outlook-tree").html(menus);
			$("#left-menu-tree").show();
			$("#resize-bar").show();
			adjustWidth();
		}else{
			$("#left-menu-tree").hide();
			$("#resize-bar").hide();
			adjustWidth();
		}
		if(id){
			setNavMenuStyle(id, "MENU_BAR");
		}
	}else{
		if(id){
			setLeftMenuStyle(id, "outlook-tree");
		}
	}
	if(url && url != 'null'){
		document.getElementById("stage").src = path+url;
	}
}

function showUserInfo(){
	jQuery.ajax({
		url:path+'/getCurrentUser.action',
		type:'POST',
		dataType: "json",
		success:function(json){
			var user = json.data;
			var html = [];
			if(user){
				html.push("<a href=\"javascript:void(0);\" onclick=\"javascript:doAction('/bm/enterUserCenter.action', '用户中心', null, -1);\"><img class=\"avatar\" SRC=\"images/index/user_32.png\" alt=\"\" /></a>");
				html.push("用户：<b><a href=\"javascript:void(0);\" onclick=\"javascript:doAction('/bm/enterUserCenter.action', '用户中心', null, -1);\">"+user.userText+"</a></b>");
				html.push("<span>|</span><a href=\"javascript:void(0);\" onclick=\"javascript:doAction('/bm/enterUserCenter.action#chgpwd', '用户中心', null, -1);\">修改密码</a>");
				html.push("<span>|</span><a href=\""+path+"/j_spring_security_logout\">注销</a><br/>");
				html.push("<small>登录次数：<b>"+user.loginTimes+"</b>");
				html.push("<br/>上次登录：<b>"+user.lastLoginTimeText+"</b>");
				html.push("</small>");
			}else{
				html.push("<a href=\"javascript:login();\">登录</a>");
			}
			$("#topnav").html(html.join(""));
		}
	});
}
function showUserMenu(callback){
	jQuery.ajax({
		url:path+'/listOfUserMenu.action',
		type:'POST',
		dataType: "json",
		success:function(json){
			var obj = json;
			if(typeof(obj) == "string")
				obj = eval(obj);
				
			window.USER_MENU_TREE = obj.data;
			window.MENU_NAME_MAP = {};
			showMenu();
			callback();
		}
	});
}

function login(fn){
	scriptLogin("/login.jsp", function(){
		showUserInfo();
		showUserMenu();
		if(fn)fn();
	}, window, 3);
}

$(document).ready(function(){
	showUserInfo();
	
	function layout(){
		adjustWidth();
		adjustHeight();
	}

	$(window).resize(layout);
	
	layout();
	
	showUserMenu(layout);
	
	$("#resize-bar").bind("mousedown", function(event){
		var oleft = event.pageX;
		var treeEl = $("#left-menu-tree");
		var width = treeEl.width();
		
		var frameWrapper = $("#content-frame");
		var mask = $("#left-hbar-drag-mask")[0];
		if(! mask){
			mask = document.createElement("DIV");
			mask.id = "left-hbar-drag-mask";
			mask.style.position = "absolute";
			mask.style.cursor = "w-resize";
			mask.style.zIndex = 1000;
			$(document.body).append(mask);
			var pos = frameWrapper.position();
			mask.style.top = pos.top+"px";
			mask.style.left = pos.left+"px";
			mask.style.width = frameWrapper.width()+"px";
			mask.style.height = frameWrapper.height()+"px";
		}
		$(document).css({"cursor": "w-resize"}).bind("mousemove", function(event){
			var left = event.pageX;
			var move = left-oleft;
			
			pos = frameWrapper.position();
			mask.style.left = pos.left+"px";
			mask.style.width = frameWrapper.width()+"px";
			
			treeEl.width(width + move);
			adjustWidth();
		});
	});
	$(document).bind("mouseup", function(event){
		$(this).css({"cursor": "default"}).unbind("mousemove");
		document.body.removeChild($("#left-hbar-drag-mask")[0]);
	});
});